<?php
/**
 * Created by PhpStorm.
 * User: Shroom Ari
 * Date: 11/21/14
 * Time: 10:22 PM
 */
class User extends MY_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->layout->setLayout('backend/index');
        $session_data = $this->session->userdata('logged_in');
        $usergroup_id = $session_data['usergroup_id'];
        if ($usergroup_id!=1) redirect('../petshop');
    }
    public function listUser($page=1){
        $data['seo']['title']='User';
        $total_news= $this->muser->countAll();
        $perpage = 5;
        $config['base_url'] = "/petshop/backend/user/listUser/"; // xác định trang phân trang
        $config['total_rows'] = $total_news; // xác định tổng số record
        $config['per_page'] = $perpage; // xác định số record ở mỗi trang
        $config['num_links'] = 10;
        $config['cur_page'] =$page;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['_config']= $config;
        $data['_page']=$page;
        $offset = ($page-1) * $perpage;
        $data['user']= $this->muser->getUser($perpage,$offset);
        if (isset($_POST['submit']) and $_POST['submit'] == 'delete'){
            $this->muser->delete();
        }
        $this->layout->view('backend/layout/user', $data);
    }
    public function delete(){
        $this->muser->delete($this->input->post('id'));
    }
    public function editUser($id){
        $data['seo']['title']='Edit User';
        $data['userById']= $this->muser->getUserById($id);
        $this->layout->view('backend/layout/edit_user', $data);

    }
    public function listUserGroup($page=1){
        $data['seo']['title']='UserGroup';
        $total_news= $this->musergroup->countAll();
        $perpage = 5;
        $config['base_url'] = "/petshop/backend/user/listUserGroup/"; // xác định trang phân trang
        $config['total_rows'] = $total_news; // xác định tổng số record
        $config['per_page'] = $perpage; // xác định số record ở mỗi trang
        $config['num_links'] = 10;
        $config['cur_page'] =$page;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['_config']= $config;
        $data['_page']=$page;
        $offset = ($page-1) * $perpage;
        $data['usergroup']= $this->musergroup->getUserGroup($perpage,$offset);
        if (isset($_POST['submit']) and $_POST['submit'] == 'delete'){
            $this->mnusergroup->delete();
        }
        $this->layout->view('backend/layout/user_group', $data);
    }

    public function deleteUserGroup(){
        $this->musergroup->delete($this->input->post('id'));
    }
    function save_image(){
        $name = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10).".jpg";

        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        if ((($_FILES["file"]["type"] == "image/gif")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/jpg")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/x-png")
                || ($_FILES["file"]["type"] == "image/png"))
            && in_array($extension, $allowedExts)) {
            $file_name = $_SERVER["DOCUMENT_ROOT"]."/petshop/public/images/".$name;
            if(file_exists($file_name)) {
                chmod($file_name,0755); //Change the file permissions if allowed
                unlink($file_name); //remove the file
            }
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"], $file_name);
                echo 1;
            }
        }
        return $name;
    }
    public function newUserGroup(){
        $data['seo']['title']='Add User Group';
        if (isset($_POST['submit']) and $_POST['submit'] == 'add'){
            $this->form_validation->set_rules('name', 'Name', 'required|min_length[3]|xss_clean|callback__title');
            $this->form_validation->set_rules('discount', 'Discount', 'required|xss_clean');
            $this->form_validation->set_rules('urlImage','Url Image', 'trim|required');
            if($this->form_validation->run())
            {
                $data = array(
                    'usergroup_id' =>'',
                    'usergroup_name' => $this->input->post('tname'),
                    'discount' => $this->input->post('discount'),
                    'url_image' => $this->save_image()
                );
                if($this->musergroup->create($data))
                {
                    redirect('backend/user/listUserGroup');
                }
            }
        }
        $this->layout->view('backend/layout/new_user_group', $data);

    }
    public function editUserGroup($id){
        $data['seo']['title']='Edit User Group';
        $data['userGroupById']= $this->musergroup->getUserGroupById($id);
        if (isset($_POST['submit']) and $_POST['submit'] == 'edit'){
            $this->form_validation->set_rules('name', 'Name', 'required|min_length[3]|xss_clean|callback__title');
            $this->form_validation->set_rules('discount', 'Discount', 'required|xss_clean');
            if($this->form_validation->run())
            {
                if ($_FILES["file"]["error"] == 0)
                $dt = array(
                    'usergroup_name' => $this->input->post('name'),
                    'discount' => $this->input->post('discount'),
                    'url_image' => $this->save_image()
                );
                else
                $dt = array(
                    'usergroup_name' => $this->input->post('name'),
                    'discount' => $this->input->post('discount'),
                );
                if($this->musergroup->updateUserGroup($id,$dt))
                {
                    redirect('backend/user/listUserGroup');
                }
            }
        }
        $this->layout->view('backend/layout/edit_user_group', $data);

    }
}